package cn.bdqn.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.bdqn.dao.RoleDao;
import cn.bdqn.dao.UserDao;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import cn.bdqn.util.PageBean;

@Service("userService")
public class UserServiceImpl implements UserService{
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Override
	public User findByCodeAndPassword(String userCode, String userPassword) {
		// TODO Auto-generated method stub
		User user=userDao.findByCode(userCode);	
		return user;
	}

	@Override
	public List<User> showAll() {
		// TODO Auto-generated method stub
		List<User> list=userDao.showAll();
		return list;
	}

	@Override
	public PageBean<User> findByPage(int pageNo, int pageSize, String userName, Integer roleId) {
		PageBean<User> p=new PageBean<User>();
		p.setPageSize(pageSize);
		int totalCount=userDao.getCountByConditions(userName, roleId);
		p.setTotalCount(totalCount);
		p.setPageNo(pageNo);
		int form=(p.getPageNo()-1)*p.getPageSize();
		List<User> pageList=userDao.findByPageConditions(form, pageSize, userName, roleId);
		p.setPageList(pageList);
		return p;
	}

	@Override
	public boolean checkUserCode(String userCode) {
		// TODO Auto-generated method stub
		User user=userDao.findByCode(userCode);
		if(user!=null){
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		int result=userDao.addUser(user);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public User findById(Integer id) {
		// TODO Auto-generated method stub
		User user=userDao.findById(id);
		return user;
	}

	@Override
	public boolean updateByUser(User user) {
		// TODO Auto-generated method stub
		int result=userDao.updateByUser(user);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public int deleteById(Integer uid) {
		// TODO Auto-generated method stub
		int result=userDao.deleteById(uid);
		return result;
	}

}
